﻿import gs.TweenLite;
import gs.TweenFilterLite;
import mx.transitions.easing.*;
import mx.utils.Delegate;

class com.hatch.effects.MouseScroll
{
	private var _view:MovieClip;
	private var _content:Array;
	private var _nodes:Array;
	private var _container;
	private var _tweenCount:Number;
	private var _tweenInterval;
	private var _currentFeature:Number;
	private var _scrollDirection:String;
	private var _me:MouseScroll;
	
	private var spin:Number = 0;
	
	function MouseScroll(view:MovieClip, content:Array, scrollDirection:String)
	{
		//trace("MouseScroll :: CONSTRUCTOR");
		_view = view;
		_me = this;
		_scrollDirection = scrollDirection;
		switch(_scrollDirection)
		{
			case "vertical" :
				_view.arrowBtns.gotoAndStop("vertical");
				break;
			case "horizontal" :
				_view.arrowBtns.gotoAndStop("horizontal");
				break;
		}
		_content = content;
		_nodes = new Array();
		_currentFeature = 0;
		_tweenCount = 0;
		build();
	}
	
	public function build():Void
	{
		_container = _view.createEmptyMovieClip("mouseScroll", _view.getNextHighestDepth());
		
		_container.createEmptyMovieClip("mask_mc", _container.getNextHightestDepth());
		_container._x = -60;
		_container._y = 163;
		
		drawRectangle(_container.mask_mc, 225, 16, 0x000000, 70);
		
		
		function drawRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number, fillColor:Number, fillAlpha:Number):Void {
		   	with (target_mc) {
		        beginFill(fillColor, fillAlpha);
		        moveTo(0, 0);
		        lineTo(boxWidth, 0);
		        lineTo(boxWidth, boxHeight);
		        lineTo(0, boxHeight);
		        lineTo(0, 0);
		        endFill();
		    }
		}
		
		_container.mask_mc._x = 50;
		_container.mask_mc._y = 0;
		_container.setMask(_container.mask_mc);
		
		
		//_container = _view;
		buildButtons();
	}
	
	private function buildButtons():Void
	{
		for(var i = 0; i < _content.length; i++)
		{
			//var mc = _container.createEmptyMovieClip("mc", _container.getNextHighestDepth());
			//var txt = mc.createTextField("tf", 1, 75, 2, 300, 30);
			var mc:MovieClip = _container.attachMovie("TopText", "mc", _container.getNextHighestDepth());
			var txt:TextField = mc.topText_txt;
			var format:TextFormat = new TextFormat();
			//
			mc._yscale = 90;
			txt._x = 72;
			//txt._y = 1;
			//txt.multiline = false;
			//txt.border = true;
			//txt.thickness = 400;
			//txt.sharpness = -100;
			txt.embedFonts = true;
			txt.text = _content[i];
			//
			format.font = "Helvetica";
			format.kerning = true;
			format.letterSpacing = -.9;
			format.bold = true;
			format.size = 12.75;
			//
			txt.setTextFormat(format);
				//
			switch(_scrollDirection)
			{
				case "vertical" :
					mc._y = -mc._height*2;
					break;
				case "horizontal" :
					mc._x = -mc._width*2;
					break;
			}
			//txt.autoSize = "center";
			//txt.selectable = false;
		
			
			//mc._alpha = 0;
			_nodes.push(mc);
		}
	}
	
	public function init():Void
	{
		//--Build Definition Text Field
		_view.defined_mc.defSpin_mc.createTextField("defined_txt", 1, 0, 10, 150, 300);
		_view.defined_mc.defSpin_mc.defined_txt.text = "You retire,\nyour money\nkeeps working.";
		
		var defFormat:TextFormat = new TextFormat();
		defFormat.font = "Helvetica";
		defFormat.size = 14;
		_view.defined_mc.defSpin_mc.defined_txt.setTextFormat(defFormat);
			
		//--Initial Cycle
		_tweenInterval = setInterval(this, "cycleTween", 200);	
	}
	
	private function cycleTween():Void
	{
		trace("cycle tween")
		
		if(_tweenCount < 6)
		{
			//tweenRetreat();
			tweenAdvance();
		}
		else
		{
			clearInterval(_tweenInterval);
		}
		
	}
	
	public function tweenAdvance():Void
	{
		switch(_scrollDirection)
		{
			case "vertical" :
				tweenDown();
				break;
			case "horizontal" :
				tweenRight();
				break;
		}
	}
	
	public function tweenRetreat():Void
	{
		switch(_scrollDirection)
		{
			case "vertical" :
				tweenUp();
				break;
			case "horizontal" :
				tweenLeft();
				break;
		}
	}


	public function tweenRight():Void
	{	
		_tweenCount++;
		var prevNode:MovieClip = _nodes[_currentFeature];
		
		if(_currentFeature + 1 < _nodes.length)
		{
			var nextNode:MovieClip = _nodes[_currentFeature + 1];
			_currentFeature++;
		}
		else
		{
			var nextNode:MovieClip = _nodes[0];
			_currentFeature = 0;
		}
		//tween previous node to the right
		TweenLite.to(prevNode, .6, {_x: prevNode._width*3, _alpha: 0, overwrite:true})
		//tween next node to the right
		nextNode._x = -nextNode._width*2;
		TweenLite.to(nextNode, .7, {_x: 0, _alpha:100, overwrite: false});	
	}
	
	public function tweenLeft():Void
	{
		_tweenCount++;
		//trace("MouseScroll :: tweenLeft() :: _currentFeature = " + _currentFeature)
		var prevNode:MovieClip = _nodes[_currentFeature];
		
		if(_currentFeature - 1 < 0)
		{
			var nextNode:MovieClip = _nodes[_nodes.length-1];
			_currentFeature = _nodes.length-1;
		}
		else
		{
			var nextNode:MovieClip = _nodes[_currentFeature - 1];
			_currentFeature--;
		}
		//tween previous node to the right
		TweenLite.to(prevNode, .6, {_x: -prevNode._width*3, _alpha: 0,  overwrite:true})
		//tween next node to the right
		nextNode._x = nextNode._width*2;
		TweenLite.to(nextNode, .7, {_x: 0, _alpha: 100,  overwrite: false});
	}
	
	
	public function tweenDown():Void
	{	
		_tweenCount++;
		var prevNode:MovieClip = _nodes[_currentFeature];
		
		if(_currentFeature + 1 < _nodes.length)
		{
			var nextNode:MovieClip = _nodes[_currentFeature + 1];
			_currentFeature++;
		}
		else
		{
			var nextNode:MovieClip = _nodes[0];
			_currentFeature = 0;
		}
		
		//tween previous node to the right
		TweenLite.to(prevNode, .6, {_y: prevNode._width*3, _alpha: 0, overwrite:true});
		TweenLite.to(_view.defined_mc.defSpin_mc, .2, {_y: 0, _alpha: 100, overwrite:true});
		//tween next node to the right
		nextNode._y = -nextNode._height*2;
		_view.defined_mc.defSpin_mc._y = -nextNode._height*2;
		
		TweenLite.to(_view.defined_mc.defSpin_mc, .2, {_y:20, _alpha:100, overwrite: true, ease: Regular.easeOut, onComplete:resetMe, onCompleteScope:_me });
		TweenLite.to(_view.defined_mc.defSpin_mc, .2, {_y:0, delay:.3, _alpha:100, overwrite: false, ease: Regular.easeOut });
		
		TweenLite.to(nextNode, .2, {_y: 20, _alpha:100, overwrite: false, ease: Regular.easeOut, onComplete:resetMe2, onCompleteParams:[nextNode], onCompleteScope:_me  });
		TweenLite.to(nextNode, .2, {_y:4, delay:.3, _alpha:100, overwrite: false, ease: Regular.easeOut });
		
		TweenFilterLite.to( _view.mouseScroll, 0.15, {blurFilter:{blurX:2, blurY:4}, overwrite:true, onComplete:Delegate.create( this, phaseOutBlur ) } );
		TweenFilterLite.to( _view.defined_mc, 0.15, {blurFilter:{blurX:2, blurY:4}, overwrite:true, onComplete:Delegate.create( this, phaseOutBlur ) } );
	}
	
	private function phaseOutBlur(){
		trace("PHASE OUT BLUR CALLED")
		TweenFilterLite.to( _view.mouseScroll, 0.3, {blurFilter:{blurX:0, blurY:0}, overwrite:true } );
		TweenFilterLite.to( _view.defined_mc, 0.3, {blurFilter:{blurX:0, blurY:0}, overwrite:true } );
	}
	
	private function resetMe()
	{
		_view.defined_mc.defSpin_mc._y -= 60;
	}
	
	private function resetMe2(myNode:MovieClip)
	{
		var _myNode:MovieClip = myNode;
		_myNode._y -= 60;
		trace(_myNode);
		trace(_myNode._y);
	}
	
	public function tweenUp():Void
	{
		_tweenCount++;
		//trace("MouseScroll :: tweenLeft() :: _currentFeature = " + _currentFeature)
		var prevNode:MovieClip = _nodes[_currentFeature];
		
		if(_currentFeature - 1 < 0)
		{
			var nextNode:MovieClip = _nodes[_nodes.length-1];
			_currentFeature = _nodes.length-1;
		}
		else
		{
			var nextNode:MovieClip = _nodes[_currentFeature - 1];
			_currentFeature--;
		}
		//tween previous node to the right
		TweenLite.to(prevNode, .6, {_y: -prevNode._height*3, _alpha: 0,  overwrite:true})
		//tween next node to the right
		nextNode._y = nextNode._height*2;
		TweenLite.to(nextNode, .7, {_y: 0, _alpha: 100,  overwrite: false, ease: Elastic.easeOut});
	}
	
	
	
	
}